using Engine;
using System.Xml.Linq;
using SCIENEW;

namespace Game {
    public class SiftingRecipeWidget : CanvasWidget {
        public LabelWidget m_nameWidget;

        public LabelWidget m_descriptionWidget;

        public GridPanelWidget m_gridWidget;

        public IECraftingRecipeSlotWidget m_resultWidget;

        public CraftingRecipe m_recipe;

        public string m_nameSuffix;

        public bool m_dirty = true;

        public string NameSuffix {
            get { return m_nameSuffix; }
            set {
                if (value != m_nameSuffix) {
                    m_nameSuffix = value;
                    m_dirty = true;
                }
            }
        }

        public CraftingRecipe Recipe {
            get { return m_recipe; }
            set {
                if (value != m_recipe) {
                    m_recipe = value;
                    m_dirty = true;
                }
            }
        }

        public SiftingRecipeWidget() {
            XElement node = ContentManager.Get<XElement>("Widgets/SiftingRecipeWidget");
            LoadContents(this, node);
            m_nameWidget = Children.Find<LabelWidget>("SiftingRecipeWidget.Name");
            m_descriptionWidget = Children.Find<LabelWidget>("SiftingRecipeWidget.Description");
            m_gridWidget = Children.Find<GridPanelWidget>("SiftingRecipeWidget.Ingredients");
            m_resultWidget = Children.Find<IECraftingRecipeSlotWidget>("SiftingRecipeWidget.Result");
            m_resultWidget.CustomLabel = true;
            m_resultWidget.m_labelWidget.IsVisible = false;
            for (int i = 0; i < m_gridWidget.RowsCount; i++) {
                for (int j = 0; j < m_gridWidget.ColumnsCount; j++) {
                    var widget = new IECraftingRecipeSlotWidget();
                    widget.CustomLabel = true;
                    widget.m_labelWidget.FontScale = 0.75f;
                    widget.m_labelWidget.IsVisible = true;
                    m_gridWidget.Children.Add(widget);
                    m_gridWidget.SetWidgetCell(widget, new Point2(j, i));
                }
            }
        }

        public override void MeasureOverride(Vector2 parentAvailableSize) {
            if (m_dirty) {
                UpdateWidgets();
            }
            base.MeasureOverride(parentAvailableSize);
        }

        public void UpdateWidgets() {
            m_dirty = false;
            if (m_recipe != null) {
                Dictionary<string, float> resultsCount = new();
                HashSet<string> results = m_recipe.Ingredients.Where(x => x != null).ToHashSet();
                int maxCount = -1;
                foreach (var result in results) {
                    int count = m_recipe.Ingredients.Count(x => x == result);
                    resultsCount.Add(result, count);
                    maxCount = Math.Max(maxCount, count);
                }
                foreach (var result in resultsCount) {
                    resultsCount[result.Key] = result.Value / maxCount;
                }

                Block block = BlocksManager.Blocks[Terrain.ExtractContents(m_recipe.ResultValue)];
                m_nameWidget.Text = block.GetDisplayName(null, m_recipe.ResultValue) + ((!string.IsNullOrEmpty(NameSuffix)) ? NameSuffix : string.Empty);
                m_descriptionWidget.Text = m_recipe.Description;
                m_nameWidget.IsVisible = true;
                m_descriptionWidget.IsVisible = true;

                int index = 0;
                foreach (IECraftingRecipeSlotWidget child in m_gridWidget.Children) {
                    if (index > resultsCount.Count - 1) {
                        child.SetIngredient(null);
                        child.m_labelWidget.Text = string.Empty;
                        continue;
                    }
                    child.SetIngredient(resultsCount.ElementAt(index).Key);
                    child.m_labelWidget.Text = ((int)(resultsCount.ElementAt(index).Value * 100)).ToString() + '%';
                    index++;
                }
                m_resultWidget.SetResult(m_recipe.ResultValue, m_recipe.ResultCount);
            }
            else {
                m_nameWidget.IsVisible = false;
                m_descriptionWidget.IsVisible = false;
                foreach (IECraftingRecipeSlotWidget child2 in m_gridWidget.Children) {
                    child2.SetIngredient(null);
                    child2.m_labelWidget.Text = string.Empty;
                }
                m_resultWidget.SetResult(0, 0);
            }
        }
    }
}